package com.wangjiyuan.giftshuo.ui.activity.other.more;

import android.animation.ObjectAnimator;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.wangjiyuan.giftshuo.R;
import com.wangjiyuan.giftshuo.adapter.MoreAdapter;
import com.wangjiyuan.giftshuo.base.BaseActivity;
import com.wangjiyuan.giftshuo.bean.more.MoreBean;
import com.wangjiyuan.giftshuo.bean.more.PostsBean;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MoreActivity extends BaseActivity implements View.OnClickListener, MoreContract.MoreView {
    private ImageView back, share, top_image, arrow;
    private XRecyclerView more_list;
    private Toolbar toolbar;
    private MoreAdapter adapter;
    private MoreBean bean;
    private List<PostsBean> postlist;
    private int offset = 0;
    private TextView column_name, count, title, add_attention, introduction, show_all;
    private MorePresenter presenter;
    private int id;
    private int height;
    private int distance_to_top;
    private boolean flag;
    private int lineCount;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_more);
        Intent intent = getIntent();
        id = intent.getIntExtra("id", -1);
        presenter = new MorePresenter(this);
        initView();
        initDate();
    }

    private void initDate() {
        Map<String, String> map = new HashMap<>();
        map.put("limit", "20");
        map.put("offset", String.valueOf(offset));
        presenter.getMoreBean(String.valueOf(id), map);
    }

    private void initView() {
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        back = (ImageView) findViewById(R.id.back);
        back.setOnClickListener(this);
        title = (TextView) findViewById(R.id.title);
        share = (ImageView) findViewById(R.id.share);
        share.setOnClickListener(this);
        more_list = (XRecyclerView) findViewById(R.id.more_list);
        more_list.setPullRefreshEnabled(false);
        postlist = new ArrayList<>();
        setRecycler();
        setHead();
    }

    private void setHead() {
        View view = getLayoutInflater().inflate(R.layout.item_more_head, more_list, false);
        column_name = (TextView) view.findViewById(R.id.column_name);
        count = (TextView) view.findViewById(R.id.count);
        arrow = (ImageView) view.findViewById(R.id.arrow);
        add_attention = (TextView) view.findViewById(R.id.add_attention);
        add_attention.setOnClickListener(this);
        top_image = (ImageView) view.findViewById(R.id.top_image);
        introduction = (TextView) view.findViewById(R.id.introduction);
        introduction.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
               lineCount = introduction.getLineCount();
                if (lineCount <= 2) {
                    show_all.setVisibility(View.GONE);
                    arrow.setVisibility(View.GONE);

                } else {
                    flag = false;
                }
            }
        });
        show_all = (TextView) view.findViewById(R.id.show_all);
        show_all.setOnClickListener(this);
        more_list.addHeaderView(view);

    }

    private void setRecycler() {
        adapter = new MoreAdapter(this, postlist);
        LinearLayoutManager manager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        more_list.setAdapter(adapter);
        more_list.setLayoutManager(manager);
        more_list.addOnScrollListener(new RecyclerView.OnScrollListener() {


            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                if (height == 0) {
                    height = toolbar.getHeight();
                }
                distance_to_top += dy;
                if (height == 0) {
                    return;
                }
                if (distance_to_top == 0) {
                    title.setTextColor(0x00000000);
                    toolbar.setBackgroundColor(0x00ffffff);
                    back.setImageResource(R.mipmap.btn_back_white);
                    share.setImageResource(R.mipmap.icon_share_white);
                } else if (distance_to_top > height) {
                    title.setTextColor(0xff000000);
                    toolbar.setBackgroundColor(0xffffffff);
                    back.setImageResource(R.mipmap.btn_back_grey);
                    share.setImageResource(R.mipmap.icon_share_black);
                } else if (distance_to_top < height) {
                    int i = height / 8;
                    int txtcolor = 0xff000000 - (height - distance_to_top) / i * 0x1f000000;
                    title.setTextColor(txtcolor);
                    toolbar.setBackgroundColor(txtcolor + 0xffffff);
                }

            }

            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                switch (newState) {
                    case RecyclerView.SCROLL_STATE_IDLE:
                        if (bean != null && bean.getData().getPaging().getNextUrl().isEmpty()) {
                            initDate();
                        }
                        break;
                }
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.add_attention:
                break;
            case R.id.show_all:
                if (!flag) {
                    introduction.setEllipsize(null);
                    introduction.setMaxLines(lineCount);
                    show_all.setText("收起");
                    ObjectAnimator.ofFloat(arrow, "rotation", 180f)
                            .setDuration(200)
                            .start();
                    flag = true;
                } else {
                    introduction.setMaxLines(2);
                    introduction.setEllipsize(TextUtils.TruncateAt.END);
                    show_all.setText(R.string.all);
                    ObjectAnimator.ofFloat(arrow, "rotation", 360f)
                            .setDuration(200)
                            .start();
                    flag = false;
                }
                break;
            case R.id.back:
                finish();
                break;
            case R.id.share:
                Toast.makeText(this, "分享", Toast.LENGTH_SHORT).show();
                break;
        }
    }

    @Override
    public void getMoreBean(MoreBean bean) {
        this.bean = bean;
        postlist.addAll(bean.getData().getPosts());
        adapter.notifyDataSetChanged();
        offset += 20;
        column_name.setText(bean.getData().getTitle());
        count.setText(bean.getData().getLikesCount() + "");
        title.setText(bean.getData().getTitle() + "");

        introduction.setText(bean.getData().getDescription());
        Glide.with(this)
                .load(bean.getData().getCoverImageUrl())
                .placeholder(getResources().getDrawable(R.mipmap.post_place_holder_image))
                .centerCrop()
                .into(top_image);

    }
}
